/*
	Title: 02_Table7.do
	Paper: Brauning, Ivashina, Ozdagli (2025)
	Description: Table 7 - Investment and binding covenants
	Note: Uses Version 5 of reghdfe
*/

/*------------------------------------------------------------------------------
								  Table 7
------------------------------------------------------------------------------*/
// Import Table 7 data
use "$data_path/Table7.dta", clear 


// Table Setup -----------------------------------------------------------------
putdocx clear 
putdocx begin, landscape
putdocx paragraph
putdocx table table7 = (21, 9), border(insideV, nil) border(insideH, nil) border(left, nil) border(right, nil)

* Left margin
putdocx table table7(5,1) = ("Bind")
putdocx table table7(7,1) = ("Incurrence bind")
putdocx table table7(9,1) = ("Maintenance bind")
putdocx table table7(12,1) = ("Observations")
putdocx table table7(13,1) = ("R-squared")
putdocx table table7(14,1) = ("Firm FE")
putdocx table table7(15,1) = ("Quarter FE")
putdocx table table7(16,1) = ("Controls")
putdocx table table7(17,1) = ("Controls*Cov-type")
putdocx table table7(18,1) = ("Quarter*Cov-type FE")
putdocx table table7(20,1) = ("H0: Incurrence=Maintenance")
putdocx table table7(21,1) = ("p-value")

* Top margin
putdocx table table7(1,2), colspan(8)
putdocx table table7(1,2) = ("Investment (% Capital)")
putdocx table table7(1,2), border(bottom)

putdocx table table7(2,4) = ("Incurrence only")
putdocx table table7(2,6) = ("Maintenance only")
putdocx table table7(2,9) = ("Both types")

putdocx table table7(3,2) = ("(1)")
putdocx table table7(3,3) = ("(2)")
putdocx table table7(3,4) = ("(3)")
putdocx table table7(3,5) = ("(4)")
putdocx table table7(3,6) = ("(5)")
putdocx table table7(3,7) = ("(6)")
putdocx table table7(3,8) = ("(7)")
putdocx table table7(3,9) = ("(8)")
putdocx table table7(3,.), border(bottom)


// Define Regression Specifications --------------------------------------------
local LHS "capex_capital_ratio_tr"

local controls0   "L_macroq_tr L_logatq_tr cashflowq_tr" 
local dd_controls " L_DD_Total_final_tr L_DD_Total_final_sq_tr"
local controls    "`controls0' `dd_controls'"

local cond0 "L.dealscan <= 1"
local cond1 "_b1_diff_std <= 2 & _b2_diff_std <= 2"
local cond2 "(!mi(lev_limit) | !mi(intcov_limit) | !mi(Ilev_limit) | !mi(Iintcov_limit))"
local conditions "`cond0' & `cond1' & `cond2'"


// Column (1) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' c.(L_in_covenant_violation) `controls' L_incurrence L_maintenance if `conditions', ///
			  version(5) nowarn ///
			  absorb(xdim qdate) cluster(xdim qdate)

// Store results 
matrix reg_results = r(table)
local violation_b : display %04.2f reg_results["b", "L_in_covenant_violation"]  
local violation_se : display %04.2f reg_results["se", "L_in_covenant_violation"]
local violation_p = reg_results["pvalue", "L_in_covenant_violation"]

if (`violation_p' > 0.1) {
	local violation_sigstars = ""
} 
if (`violation_p' < 0.1) {
	local violation_sigstars = "*"
}
if (`violation_p' < 0.05) {
	local violation_sigstars = "**"
}
if (`violation_p' < 0.01) {
	local violation_sigstars = "***"
}
local N = e(N)
local r2 = e(r2)

// Put results into table in Word Document 
putdocx table table7(5,2) = ("`violation_b'`violation_sigstars'")
putdocx table table7(6,2) = ("(`violation_se')")
putdocx table table7(12,2) = (`N')
putdocx table table7(13,2) = (`r2')
putdocx table table7(14,2) = ("Yes")
putdocx table table7(15,2) = ("Yes")
putdocx table table7(16,2) = ("Yes")
putdocx table table7(17,2) = ("No")
putdocx table table7(18,2) = ("No")

 
// Column (2) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' L_Iin_covenant_violation `controls' L_incurrence L_maintenance if `conditions', ///
			  version(5) nowarn ///
			  absorb(xdim qdate) cluster(xdim qdate)

// Store results 
matrix reg_results = r(table)
local incurrence_b : display %04.2f reg_results["b", "L_Iin_covenant_violation"]  
local incurrence_se : display %04.2f reg_results["se", "L_Iin_covenant_violation"]
local incurrence_p = reg_results["pvalue", "L_Iin_covenant_violation"]

if (`incurrence_p' > 0.1) {
	local incurrence_sigstars = ""
} 
if (`incurrence_p' < 0.1) {
	local incurrence_sigstars = "*"
}
if (`incurrence_p' < 0.05) {
	local incurrence_sigstars = "**"
}
if (`incurrence_p' < 0.01) {
	local incurrence_sigstars = "***"
}
local N = e(N)
local r2 = e(r2)

// Put results into table in Word Document 
putdocx table table7(7,3) = ("`incurrence_b'`incurrence_sigstars'")
putdocx table table7(8,3) = ("(`incurrence_se')")
putdocx table table7(12,3) = (`N')
putdocx table table7(13,3) = (`r2')
putdocx table table7(14,3) = ("Yes")
putdocx table table7(15,3) = ("Yes")
putdocx table table7(16,3) = ("Yes")
putdocx table table7(17,3) = ("No")
putdocx table table7(18,3) = ("No")


// Column (3) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' L_Iin_covenant_violation `controls' L_incurrence L_maintenance if `conditions' & incurrence == 1 & maintenance == 0, ///
			  version(5) nowarn ///
			  absorb(xdim qdate) cluster(xdim qdate)
		
// Store results 
matrix reg_results = r(table)
local incurrence_b : display %04.2f reg_results["b", "L_Iin_covenant_violation"]   
local incurrence_se : display %04.2f reg_results["se", "L_Iin_covenant_violation"]
local incurrence_p = reg_results["pvalue", "L_Iin_covenant_violation"]

if (`incurrence_p' > 0.1) {
	local incurrence_sigstars = ""
} 
if (`incurrence_p' < 0.1) {
	local incurrence_sigstars = "*"
}
if (`incurrence_p' < 0.05) {
	local incurrence_sigstars = "**"
}
if (`incurrence_p' < 0.01) {
	local incurrence_sigstars = "***"
}
local N = e(N)
local r2 = e(r2)

// Put results into table in Word Document 
putdocx table table7(7,4) = ("`incurrence_b'`incurrence_sigstars'")
putdocx table table7(8,4) = ("(`incurrence_se')")
putdocx table table7(12,4) = (`N')
putdocx table table7(13,4) = (`r2')
putdocx table table7(14,4) = ("Yes")
putdocx table table7(15,4) = ("Yes")
putdocx table table7(16,4) = ("Yes")
putdocx table table7(17,4) = ("No")
putdocx table table7(18,4) = ("No")


// Column (4) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' L_Min_covenant_violation `controls' L_incurrence L_maintenance if  `conditions', ///
			  version(5) nowarn ///
			  absorb(xdim qdate) cluster(xdim qdate)
		
// Store results 
matrix reg_results = r(table)
local maintenance_b : display %04.2f reg_results["b", "L_Min_covenant_violation"]
local maintenance_se : display %04.2f reg_results["se", "L_Min_covenant_violation"] 
local maintenance_p = reg_results["pvalue", "L_Min_covenant_violation"]

if (`maintenance_p' > 0.1) {
	local maintenance_sigstars = ""
} 
if (`maintenance_p' < 0.1) {
	local maintenance_sigstars = "*"
}
if (`maintenance_p' < 0.05) {
	local maintenance_sigstars = "**"
}
if (`maintenance_p' < 0.01) {
	local maintenance_sigstars = "***"
}
local N = e(N)
local r2 = e(r2)

// Put results into table in Word Document 
putdocx table table7(9,5) = ("`maintenance_b'`maintenance_sigstars'")
putdocx table table7(10,5) = ("(`maintenance_se')")
putdocx table table7(12,5) = (`N')
putdocx table table7(13,5) = (`r2')
putdocx table table7(14,5) = ("Yes")
putdocx table table7(15,5) = ("Yes")
putdocx table table7(16,5) = ("Yes")
putdocx table table7(17,5) = ("No")
putdocx table table7(18,5) = ("No")


// Column (5) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' L_Min_covenant_violation `controls' L_incurrence L_maintenance if  `conditions' & incurrence == 0 & maintenance == 1, ///
			  version(5) nowarn ///
			  absorb(xdim qdate) cluster(xdim qdate)
		
// Store results 
matrix reg_results = r(table)
local maintenance_b : display %04.2f reg_results["b", "L_Min_covenant_violation"]
local maintenance_se : display %04.2f reg_results["se", "L_Min_covenant_violation"]
local maintenance_p = reg_results["pvalue", "L_Min_covenant_violation"]

if (`maintenance_p' > 0.1) {
	local maintenance_sigstars = ""
} 
if (`maintenance_p' < 0.1) {
	local maintenance_sigstars = "*"
}
if (`maintenance_p' < 0.05) {
	local maintenance_sigstars = "**"
}
if (`maintenance_p' < 0.01) {
	local maintenance_sigstars = "***"
}
local N = e(N)
local r2 = e(r2)

// Put results into table in Word Document 
putdocx table table7(9,6) = ("`maintenance_b'`maintenance_sigstars'")
putdocx table table7(10,6) = ("(`maintenance_se')")
putdocx table table7(12,6) = (`N')
putdocx table table7(13,6) = (`r2')
putdocx table table7(14,6) = ("Yes")
putdocx table table7(15,6) = ("Yes")
putdocx table table7(16,6) = ("Yes")
putdocx table table7(17,6) = ("No")
putdocx table table7(18,6) = ("No")


// Column (6) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' L_Iin_covenant_violation L_Min_covenant_violation `controls' L_incurrence L_maintenance if `conditions', ///
			  version(5) nowarn ///
			  absorb(xdim qdate) cluster(xdim qdate)
		
// Store incurrence results
matrix reg_results = r(table)
local incurrence_b : display %04.2f reg_results["b", "L_Iin_covenant_violation"]
local incurrence_se : display %04.2f reg_results["se", "L_Iin_covenant_violation"]
local incurrence_p = reg_results["pvalue", "L_Iin_covenant_violation"]

if (`incurrence_p' > 0.1) {
	local incurrence_sigstars = ""
} 
if (`incurrence_p' < 0.1) {
	local incurrence_sigstars = "*"
}
if (`incurrence_p' < 0.05) {
	local incurrence_sigstars = "**"
}
if (`incurrence_p' < 0.01) {
	local incurrence_sigstars = "***"
}

// Store maintenance results
local maintenance_b : display %04.2f reg_results["b", "L_Min_covenant_violation"]
local maintenance_se : display %04.2f reg_results["se", "L_Min_covenant_violation"]
local maintenance_p = reg_results["pvalue", "L_Min_covenant_violation"]

if (`maintenance_p' > 0.1) {
	local maintenance_sigstars = ""
} 
if (`maintenance_p' < 0.1) {
	local maintenance_sigstars = "*"
}
if (`maintenance_p' < 0.05) {
	local maintenance_sigstars = "**"
}
if (`maintenance_p' < 0.01) {
	local maintenance_sigstars = "***"
}
local N = e(N)
local r2 = e(r2)

* H0: Incurrence = Maintenance (results stored in r(estimate) and r(p))
lincom L_Min_covenant_violation - L_Iin_covenant_violation
local estimate = r(estimate)
local p = r(p)

// Put results into table in Word Document 
putdocx table table7(7,7) = ("`incurrence_b'`incurrence_sigstars'")
putdocx table table7(8,7) = ("(`incurrence_se')")
putdocx table table7(9,7) = ("`maintenance_b'`maintenance_sigstars'")
putdocx table table7(10,7) = ("(`maintenance_se')")
putdocx table table7(12,7) = (`N')
putdocx table table7(13,7) = (`r2')
putdocx table table7(14,7) = ("Yes")
putdocx table table7(15,7) = ("Yes")
putdocx table table7(16,7) = ("Yes")
putdocx table table7(17,7) = ("No")
putdocx table table7(18,7) = ("No")
putdocx table table7(20,7) = (`estimate')
putdocx table table7(21,7) = (`p')


// Column (7) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' L_Iin_covenant_violation L_Min_covenant_violation (L_incurrence L_maintenance)##c.(`controls') if `conditions', ///
			  version(5) nowarn ///
			  absorb(xdim L_incurrence#qdate L_maintenance#qdate) cluster(xdim qdate)
		
// Store incurrence results
matrix reg_results = r(table)
local incurrence_b : display %04.2f reg_results["b", "L_Iin_covenant_violation"]   
local incurrence_se : display %04.2f reg_results["se", "L_Iin_covenant_violation"]
local incurrence_p = reg_results["pvalue", "L_Iin_covenant_violation"]

if (`incurrence_p' > 0.1) {
	local incurrence_sigstars = ""
} 
if (`incurrence_p' < 0.1) {
	local incurrence_sigstars = "*"
}
if (`incurrence_p' < 0.05) {
	local incurrence_sigstars = "**"
}
if (`incurrence_p' < 0.01) {
	local incurrence_sigstars = "***"
}

// Store maintenance results
local maintenance_b : display %04.2f reg_results["b", "L_Min_covenant_violation"]
local maintenance_se : display %04.2f reg_results["se", "L_Min_covenant_violation"]
local maintenance_p = reg_results["pvalue", "L_Min_covenant_violation"]

if (`maintenance_p' > 0.1) {
	local maintenance_sigstars = ""
} 
if (`maintenance_p' < 0.1) {
	local maintenance_sigstars = "*"
}
if (`maintenance_p' < 0.05) {
	local maintenance_sigstars = "**"
}
if (`maintenance_p' < 0.01) {
	local maintenance_sigstars = "***"
}
local N = e(N)
local r2 = e(r2)

* H0: Incurrence = Maintenance (results stored in r(estimate) and r(p))
lincom L_Min_covenant_violation - L_Iin_covenant_violation
local estimate = r(estimate)
local p = r(p)

// Put results into table in Word Document 
putdocx table table7(7,8) = ("`incurrence_b'`incurrence_sigstars'")
putdocx table table7(8,8) = ("(`incurrence_se')")
putdocx table table7(9,8) = ("`maintenance_b'`maintenance_sigstars'")
putdocx table table7(10,8) = ("(`maintenance_se')")
putdocx table table7(12,8) = (`N')
putdocx table table7(13,8) = (`r2')
putdocx table table7(14,8) = ("Yes")
putdocx table table7(15,8) = ("Yes")
putdocx table table7(16,8) = ("Yes")
putdocx table table7(17,8) = ("Yes")
putdocx table table7(18,8) = ("Yes")
putdocx table table7(20,8) = (`estimate')
putdocx table table7(21,8) = (`p')


// Column (8) ------------------------------------------------------------------
// Regression: 
reghdfe `LHS' L_Iin_covenant_violation L_Min_covenant_violation (L_incurrence L_maintenance)##c.(`controls') if `conditions' & incurrence == 1 & maintenance == 1, ///
			  version(5) nowarn ///
			  absorb(xdim L_incurrence#qdate L_maintenance#qdate) cluster(xdim qdate)

// Store incurrence results
matrix reg_results = r(table)
local incurrence_b : display %04.2f reg_results["b", "L_Iin_covenant_violation"]   
local incurrence_se : display %04.2f reg_results["se", "L_Iin_covenant_violation"]
local incurrence_p = reg_results["pvalue", "L_Iin_covenant_violation"]
local incurrence_sigstars
if (`incurrence_p' > 0.1) {
	local incurrence_sigstars = ""
} 
if (`incurrence_p' < 0.1) {
	local incurrence_sigstars = "*"
}
if (`incurrence_p' < 0.05) {
	local incurrence_sigstars = "**"
}
if (`incurrence_p' < 0.01) {
	local incurrence_sigstars = "***"
}

// Store maintenance results
local maintenance_b : display %04.2f reg_results["b", "L_Min_covenant_violation"]
local maintenance_se : display %04.2f reg_results["se", "L_Min_covenant_violation"]
local maintenance_p = reg_results["pvalue", "L_Min_covenant_violation"]
local maintenance_sigstars
if (`maintenance_p' > 0.1) {
	local maintenance_sigstars = ""
} 
if (`maintenance_p' < 0.1) {
	local maintenance_sigstars = "*"
}
if (`maintenance_p' < 0.05) {
	local maintenance_sigstars = "**"
}
if (`maintenance_p' < 0.01) {
	local maintenance_sigstars = "***"
}
local N = e(N)
local r2 = e(r2)

* H0: Incurrence = Maintenance (results stored in r(estimate) and r(p))
lincom L_Min_covenant_violation - L_Iin_covenant_violation
local estimate = r(estimate)
local p = r(p)

// Put results into table in Word Document
putdocx table table7(7,9) = ("`incurrence_b'`incurrence_sigstars'")
putdocx table table7(8,9) = ("(`incurrence_se')")
putdocx table table7(9,9) = ("`maintenance_b'`maintenance_sigstars'")
putdocx table table7(10,9) = ("(`maintenance_se')")
putdocx table table7(12,9) = (`N')
putdocx table table7(13,9) = (`r2')
putdocx table table7(14,9) = ("Yes")
putdocx table table7(15,9) = ("Yes")
putdocx table table7(16,9) = ("Yes")
putdocx table table7(17,9) = ("Yes")
putdocx table table7(18,9) = ("Yes")
putdocx table table7(20,9) = (`estimate')
putdocx table table7(21,9) = (`p')


// Table Setup (end) -----------------------------------------------------------
putdocx table table7(1 3/21.,2/9), halign(center)
putdocx table table7(.,1), width(3 in)

* Number formatting
putdocx table table7(6,2/9), nformat(%04.2f)
putdocx table table7(8,2/9), nformat(%04.2f)
putdocx table table7(10,2/9), nformat(%04.2f)
putdocx table table7(12,2/9), nformat(%9.0fc)
putdocx table table7(13,2/9), nformat(%9.2g)
putdocx table table7(20,2/9), nformat(%05.3f)
putdocx table table7(21,2/9), nformat(%9.4g)

* Table-wide formatting
putdocx table table7(.,.), font("Times New Roman", 9)

// Export Table 7
putdocx save "$tables_path/Table7.docx", replace
